package com.evil.account.util;

import com.evil.account.pojo.entity.User;
import com.evil.common.core.enums.RCodeEnum;
import com.evil.common.core.enums.SwitchEnum;
import com.evil.common.core.exception.BusinessException;
import org.apache.commons.lang3.StringUtils;

public class UserUtil {

    /**
     * 验证使用户是否有效
     *
     * @param user user
     * @return User
     */
    public static User checkUser(User user) {
        try {
            UserUtil.validUser(user);
        } catch (Exception e) {
            return null;
        }
        return user;
    }

    /**
     * 验证使用户是否有效
     *
     * @param user user
     */
    public static void validUser(User user) {
        if (user == null) {
            throw new BusinessException(RCodeEnum.ERROR, "找不到用户");
        }

        if (user.getIsDeleted() == null || SwitchEnum.isOpen(user.getIsDeleted())) {
            throw new BusinessException(RCodeEnum.ERROR, "用户已被删除");
        }

        if (user.getIsEnable() == null || SwitchEnum.isClose(user.getIsEnable())) {
            throw new BusinessException(RCodeEnum.ERROR, "用户已被禁用");
        }

        if (StringUtils.isBlank(user.getTel())) {
            throw new BusinessException(RCodeEnum.ERROR, "该用户手机号不存在");
        }
    }
}
